package jlb.andriva.wrappers;

import jlb.andriva.R;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ClosureExpiration {

    private String mClosurePrev;
    private String mClosurePresent;
    private String mClosureNext;
    private String mExpPrev;
    private String mExpPresent;
    private String mExpNext;
    private String mMess;

    public ClosureExpiration(Element root){
        Element table = root.select("table[cellspacing=0]").first();
        Elements tds = table.select("td");

        mClosurePrev = tds.get(5).childNode(0).toString();
        mClosurePresent = tds.get(6).childNode(0).toString();
        mClosureNext = tds.get(7).childNode(0).toString();
        mExpPrev = tds.get(9).childNode(0).toString();
        mExpPresent = tds.get(10).childNode(0).toString();
        mExpNext = tds.get(11).childNode(0).toString();
        mMess = tds.get(12).childNode(0).toString().replace("&aacute;", "a");
    }

    public LinearLayout getView(Context context){
        LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //mainParams.setMargins(2, 2, 2, 2);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        params.weight = 1;
        params.setMargins(2, 2, 2, 2);

        LinearLayout main = new LinearLayout(context);
        main.setOrientation(LinearLayout.VERTICAL);
        main.setLayoutParams(mainParams);

        LinearLayout mainTit = new LinearLayout(context);
        mainTit.setOrientation(LinearLayout.HORIZONTAL);
        mainTit.setLayoutParams(params);

        LinearLayout mainFirst = new LinearLayout(context);
        mainFirst.setOrientation(LinearLayout.HORIZONTAL);
        mainFirst.setLayoutParams(params);

        LinearLayout mainSecond = new LinearLayout(context);
        mainSecond.setOrientation(LinearLayout.HORIZONTAL);
        mainSecond.setLayoutParams(params);

        LinearLayout mainThird = new LinearLayout(context);
        mainThird.setOrientation(LinearLayout.VERTICAL);
        mainThird.setLayoutParams(params);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        LinearLayout.LayoutParams rulerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 2);

        View ruler = (View) inflater.inflate(R.layout.ruler, null);
        ruler.setLayoutParams(rulerParams);
        View ruler2 = (View) inflater.inflate(R.layout.ruler, null);
        ruler2.setLayoutParams(rulerParams);

        main.addView(mainTit);
        main.addView(ruler);
        main.addView(mainFirst);
        main.addView(mainSecond);
        main.addView(ruler2);
        main.addView(mainThird);

        //TIT

        TextView lblTit = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTit.setLayoutParams(params);
        lblTit.setEllipsize(TextUtils.TruncateAt.END);
        lblTit.setText(" ");

        mainTit.addView(lblTit);

        TextView lblTitPrev = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTitPrev.setLayoutParams(params);
        lblTitPrev.setEllipsize(TextUtils.TruncateAt.END);
        lblTitPrev.setTypeface(null, Typeface.BOLD);
        lblTitPrev.setText("Anterior");

        mainTit.addView(lblTitPrev);

        TextView lblTitPresent = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTitPresent.setLayoutParams(params);
        lblTitPresent.setEllipsize(TextUtils.TruncateAt.END);
        lblTitPresent.setTypeface(null, Typeface.BOLD);
        lblTitPresent.setText("Actual");

        mainTit.addView(lblTitPresent);

        TextView lblTitNext = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTitNext.setLayoutParams(params);
        lblTitNext.setEllipsize(TextUtils.TruncateAt.END);
        lblTitNext.setTypeface(null, Typeface.BOLD);
        lblTitNext.setText("Proximo*");

        mainTit.addView(lblTitNext);

        //FIRST

        TextView lblClosureTitle = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblClosureTitle.setLayoutParams(params);
        lblClosureTitle.setEllipsize(TextUtils.TruncateAt.END);
        lblClosureTitle.setTypeface(null, Typeface.BOLD);
        lblClosureTitle.setText("Fecha de cierre");

        mainFirst.addView(lblClosureTitle);

        TextView lblClosurePrev = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblClosurePrev.setLayoutParams(params);
        lblClosurePrev.setEllipsize(TextUtils.TruncateAt.END);
        lblClosurePrev.setText(mClosurePrev);

        mainFirst.addView(lblClosurePrev);

        TextView lblClosurePresent = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblClosurePresent.setLayoutParams(params);
        lblClosurePresent.setEllipsize(TextUtils.TruncateAt.END);
        lblClosurePresent.setText(mClosurePresent);

        mainFirst.addView(lblClosurePresent);

        TextView lblClosureNext = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblClosureNext.setLayoutParams(params);
        lblClosureNext.setEllipsize(TextUtils.TruncateAt.END);
        lblClosureNext.setText(mClosureNext);

        mainFirst.addView(lblClosureNext);

        //SECOND

        TextView lblExpTitle = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblExpTitle.setLayoutParams(params);
        lblExpTitle.setEllipsize(TextUtils.TruncateAt.END);
        lblExpTitle.setTypeface(null, Typeface.BOLD);
        lblExpTitle.setText("Fecha de vto.");

        mainSecond.addView(lblExpTitle);

        TextView lblExpPrev = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblExpPrev.setLayoutParams(params);
        lblExpPrev.setEllipsize(TextUtils.TruncateAt.END);
        lblExpPrev.setText(mExpPrev);

        mainSecond.addView(lblExpPrev);

        TextView lblExpPresent = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblExpPresent.setLayoutParams(params);
        lblExpPresent.setEllipsize(TextUtils.TruncateAt.END);
        lblExpPresent.setText(mExpPresent);

        mainSecond.addView(lblExpPresent);

        TextView lblExpNext = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblExpNext.setLayoutParams(params);
        lblExpNext.setEllipsize(TextUtils.TruncateAt.END);
        lblExpNext.setText(mExpNext);

        mainSecond.addView(lblExpNext);

        //THIRD

        TextView lblMsg = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblMsg.setLayoutParams(params);
        lblMsg.setEllipsize(TextUtils.TruncateAt.END);
        lblMsg.setText(mMess);

        mainThird.addView(lblMsg);

        return main;
    }
}
